<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018\10\11 0011
 * Time: 13:57
 */

namespace app\Lib\Ioc\AdminAuth;


use app\Lib\Model\AdminAuth\AdminMenu\AddAdminMenu;
use app\Lib\Model\AdminAuth\AdminMenu\EditAdminMenu;
use app\Lib\Model\AdminAuth\AdminMenu\GetMenuInfo;
use app\Lib\Model\AdminAuth\AdminMenu\GetMenuList;
use app\Lib\Model\AdminAuth\MenuAuth;
use app\Lib\Model\User\User;
use think\Db;

class MenuManage
{
    public function addMenu($data){
        $model = (new AddAdminMenu(new User($data['opUid'])));
        /*执行菜单添加*/
        $result = Db::transaction(function ()use ($data,$model){
            $model->setName($data['menuName']);
            $model->setArgs(isset($data['args'])?$data['args']:'');
            $model->setA($data['action']);
            $model->setProject($data['project']);
            $model->setC($data['controller']);
            $model->setMark($data['mark']);
            $model->setMenuClass($data['menuClass']);
            $model->setParentClass(new MenuAuth($data['parentId']));
            $model->setShowMenu($data['showMenu']);
            $model->setSortNo($data['sortNo']);
            $model->run();
            return true;
        });
        /*操作成功更新菜单更新时间*/
        if($result){
            $model->saveTime();
        }
        return true;
    }

    public function editMenu($data){
        $model = (new EditAdminMenu(new User($data['opUid'])));
        /*执行菜单添加*/
        $result = Db::transaction(function ()use ($data,$model){
            $model->setMenuAuth(new MenuAuth($data['id']));
            $model->setName($data['menuName']);
            $model->setArgs(isset($data['args'])?$data['args']:'');
            $model->setA($data['action']);
            $model->setProject($data['project']);
            $model->setC($data['controller']);
            $model->setMark($data['mark']);
            $model->setMenuClass($data['menuClass']);
            $model->setParentClass(new MenuAuth($data['parentId']));
            $model->setShowMenu($data['showMenu']);
            $model->setSortNo($data['sortNo']);
            $model->run();
            return true;
        });
        /*操作成功更新菜单更新时间*/
        if($result){
            $model->saveTime();
        }
        return true;
    }
    /**
     * @param $data
     * @return array
     * @throws \app\Lib\Exception\BlException
     */
    public function getMenuList($data){
        $model = new GetMenuList();
        $model->setPage($data['page']);
        $model->setKeyword($data['keyword']);
        $model->setKeywordType($data['keywordType']);
        $model->setLimit($data['limit']);
        if(isset($data['isPage'])){
            $model->setIsPage(true);
        }
        return $model->run();
    }

    public function getMenuInfo($data){
        $model = (new GetMenuInfo(new MenuAuth($data['menu_id'])));
        return  $model->run();
    }
}